package com.wechat.order.controller.SellerController;

import com.google.common.collect.Maps;
import com.wechat.order.dataobject.ProductCategory;
import com.wechat.order.dataobject.ProductInfo;
import com.wechat.order.enums.ProductStatusEnum;
import com.wechat.order.enums.ResultEnum;
import com.wechat.order.exception.SellException;
import com.wechat.order.service.CategoryService.CategoryService;
import com.wechat.order.service.ProductService.ProductService;
import com.wechat.order.util.KeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 卖家商品
 * Created by zzy on 2017/12/20.
 */
@Controller
@Slf4j
@RequestMapping("/seller/product")
public class SellProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> model){
        PageRequest pageRequest=new PageRequest(page-1,size);
        Page<ProductInfo> productInfoPage=productService.findAll(pageRequest);
        model.put("productInfoPage",productInfoPage);
        model.put("currentPage",page);
        model.put("size",size);


        return new ModelAndView("product/list",model);
    }
    @GetMapping("/on_sale")
    public ModelAndView on_sale(@RequestParam("productId") String productId,
                                Map model){
        try {
            ProductInfo productInfo = productService.findOne(productId);
            productInfo.setProductId(productId);
            productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
            productService.save(productInfo);
        }catch (SellException e){
            model.put("msg", e.getMessage());
            model.put("url","/sell/seller/product/list");
            return new ModelAndView("/common/error",model);
        }
        model.put("msg", ResultEnum.SUCCESS.getMessage());
        model.put("url","/sell/seller/product/list");

        return new ModelAndView("common/success",model);

    }
    @GetMapping("/off_sale")
    public ModelAndView off_sale(@RequestParam("productId") String productId,
                                Map model){
        try {
            ProductInfo productInfo = productService.findOne(productId);
            productInfo.setProductId(productId);
            productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
            productService.save(productInfo);
        }catch (SellException e){
            model.put("msg", e.getMessage());
            model.put("url","/sell/seller/product/list");
            return new ModelAndView("/common/error",model);
        }
        model.put("msg", ResultEnum.SUCCESS.getMessage());
        model.put("url","/sell/seller/product/list");

        return new ModelAndView("common/success",model);

    }
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId",required = false) String productId){
        ProductInfo productInfo=new ProductInfo();
        Map model = Maps.newHashMap();
        List<ProductCategory> categoryList = categoryService.findAll();
        if(null!=productId) {
             productInfo = productService.findOne(productId);

        }
        model.put("productInfo", productInfo);
        model.put("categoryList", categoryList);
        return new ModelAndView("product/index",model);
    }

    @PostMapping("/save")
        /*@CachePut(cacheNames = "Product",key = "123")*///更新
    //@CacheEvict(cacheNames = "product",key = "123")//删除
    public ModelAndView save(@ModelAttribute ProductInfo productInfo){
        Map model= Maps.newHashMap();
        try {
            productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
            if(null==productInfo.getProductId()) {
                productInfo.setProductId(KeyUtils.getUniqueKey());
            }
            productService.save(productInfo);
        }catch (SellException e){
            model.put("msg", e.getMessage());
            model.put("url","/sell/seller/product/list");
            return new ModelAndView("/common/error",model);
        }
        model.put("msg", ResultEnum.SUCCESS.getMessage());
        model.put("url","/sell/seller/product/list");

        return new ModelAndView("common/success",model);
    }
}
